package fileshare.fileprocessing;
/**
 * 
 * @author Administrator
 *
 */

import java.util.Random;

import fileshare.network.peerProcess;
public class bitmapvector
{
	private static bitmap peers[];
	private static int []data=new int[(int)peerProcess.piecenumber];
	/**
	 * the flag for the completeness of myself
	 * 
	 */
	private static boolean fileflag=false;
	private static int []map;
	
	/**
	 * 
	 * @param map
	 * @param peerindex
	 */
	public  void bitmapadd(int []map,int peerindex)
	{
		
		peers[peerindex]=new bitmap(map,peerindex);
		
	}
	/**
	 * 
	 * @param peerid
	 * @param index
	 */
	public   void changebitmap(int peerindex,int index)
	{
		log.writelog("inside changebitmap peerindex_"+peerindex+" pieceindex_"+index);
		peers[peerindex].bitmap[index]=1;
		
	}
	public bitmapvector(int peernumber)
	{
		if(map==null)
		{ map=new int[(int)peerProcess.piecenumber];
		for(int k=0;k<peerProcess.piecenumber;k++)
		{
			map[k]=0;
		}
		}
		try{
		
		peers=new bitmap[peernumber+1];
		
		for(int i=1;i<7;i++)
		{
			if(i==peerProcess.self_index)
				continue;
			bitmapadd(map,i);
			log.writelog("initialize the bitmap ofpeer"+i);
		}
		}
		catch(Exception e){e.printStackTrace();log.getExceptionTrace(e);}
		
	}
	public  int whetherintr(int selfpeerindex,int targetpeerindex)
	{
		if(fileflag)
			return -1;
		
		for(int i=0;i<peerProcess.piecenumber;i++)
		{
			if(peers[selfpeerindex].bitmap[i]==0&&peers[targetpeerindex].bitmap[i]==1)
				/*return peers[targetpeerindex].bitmap[i];*///newchange
				return i;
		}
			return -1;
		//
	}
	/**
	 * 
	 * @param selfpeerid
	 * @param targetpeerid
	 * @return if it return -1 means there is not interest piece in the target peer
	 * every time the peer receive a new piece it will call this function to determine
	 * which peer should be sent a not interested message,if the result is not -1 then it means
	 * this returned index is still interesting to me for this peer
	 * if -2 is returned means the file is complete
	 */
	public   int searchinterest(int selfpeerindex,int targetpeerindex)
	{
		if(fileflag)
			return -1;
		if(targetpeerindex==4&&peerProcess.completeflag)
			log.writelog("what ??");
		//boolean complete=(peer.self_index);
		/*if(complete)
			return -2;*/
		int flag=whetherintr(selfpeerindex,targetpeerindex);
		if(flag==-1)
			return -1;
		
		//
		Random chose=new Random();
		int indi=searchnew(selfpeerindex,targetpeerindex);
		while(true)
		{
			int i=chose.nextInt(3000)%(indi);
			if(i<0)
				continue;
			
				return data[i];
			
		}
	
		
	}
	public  int searchnew(int selfpeerindex,int targetpeerindex)
	{
		
		int indice=0;
		for(int i=0;i<peerProcess.piecenumber;i++)
		{
			if(peers[selfpeerindex].bitmap[i]==0&&peers[targetpeerindex].bitmap[i]==1)
				/*return peers[targetpeerindex].bitmap[i];*///newchange
				{data[indice]=i;
			indice++;}
		}
		return indice;
	}
	/**
	 * 
	 * @param selfpeerid
	 * @return
	 */
	public  boolean iscomplete(int selfpeerindex)
	{
		if(fileflag)
			return true;
		else
		{
		for(int i=0;i<peerProcess.piecenumber;i++)
		{
			if(peers[selfpeerindex].bitmap[i]!=1)
				return false;
		}
			fileflag=true;
			return true;
		}
		
	}
	public  void changefileflag()
	{
		fileflag=true;
	}
	 
	/**
	 * check a cetain peer whether I amcomplte now
	 */
	
	


}
